package com.managementemployees.activities;

import java.util.Calendar;

import com.managementemployees.database.DatabaseHelper;
import com.managementemployees.database.NhanVienHelper;
import com.managementemployees.fragments.FragmentNhanVien;
import com.managementemployees.model.NhanVien;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.YuvImage;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

public class SaveNhanVienActivity extends Activity {
	String TAG = "SaveNhanVienActivity";
	private EditText mEdtTenNV;
	private EditText mEdtNgaySinh;
	private RadioButton mRbNam;
	private RadioButton mRbNu;
	private RadioButton mRbPartTime;
	private RadioButton mRbFullTime;
	private Button mBtnNgaySinh;
	private Button mBtnSave;
	private Button mBtnReset;
	private Button mBtnCancel;

	private int mIdCongViec = 1;
	private String mGioiTinh = "Nu";
	private int mIdNhanVienSelected = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_nhan_vien);

		getControls();
		setUpViews();
		loadNhanVienEdit();
	}

	void getControls() {
		mEdtTenNV = (EditText) findViewById(R.id.edtTenNV);
		mRbNam = (RadioButton) findViewById(R.id.rd_nam);
		mRbNu = (RadioButton) findViewById(R.id.rd_nu);
		mRbPartTime = (RadioButton) findViewById(R.id.rd_parttime);
		mRbFullTime = (RadioButton) findViewById(R.id.rd_fulltime);
		mEdtNgaySinh = (EditText) findViewById(R.id.edtNgaySinh);
		mBtnCancel = (Button) findViewById(R.id.btnCancel);
		mBtnNgaySinh = (Button) findViewById(R.id.btnNgaySinh);
		mBtnReset = (Button) findViewById(R.id.btnReset);
		mBtnSave = (Button) findViewById(R.id.btnSaveNV);

	}

	void setUpViews() {
		mBtnSave.setOnClickListener(mOnClick);
		mBtnReset.setOnClickListener(mOnClick);
		mBtnCancel.setOnClickListener(mOnClick);
		mBtnNgaySinh.setOnClickListener(mOnClick);
		if (mRbNam.isChecked()) {
			mGioiTinh = "Nam";
		}
		if (mRbPartTime.isChecked()) {
			mIdCongViec = 0;
		}

	}

	void loadNhanVienEdit() {
		Intent i = getIntent();
		mIdNhanVienSelected = i.getIntExtra("IdNhanVien", -1);
		if (mIdNhanVienSelected > -1) {
			NhanVienHelper db = new NhanVienHelper(getBaseContext());
			NhanVien nv = db.getNhanVien(mIdNhanVienSelected);
			mEdtTenNV.setText(nv.getTenNV());
			mEdtNgaySinh.setText(nv.getNgaySinh());
//			Log.d("DD",
//					"HungND id:" + mIdNhanVienSelected + " gioitinh:"
//							+ nv.getGioiTinh());
			if (!nv.getGioiTinh().equals("Nam")) {
				mRbNu.setChecked(false);
				mRbNam.setChecked(true);
			}
			if (nv.getIdLoaiCongViec() == 1) {
				mRbFullTime.setChecked(true);
				mRbPartTime.setChecked(false);
			}

		} else {
			doReset();
		}

	}

	OnClickListener mOnClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			final int id = v.getId();
			switch (id) {
			case R.id.btnNgaySinh:
				chonNgaySinh();
				break;
			case R.id.btnSaveNV:
				doSaveNV();
				break;
			case R.id.btnReset:
				doReset();
				break;
			case R.id.btnCancel:
				doCancel();
				break;
			default:
				break;
			}
		}
	};

	void doSaveNV() {
		NhanVienHelper db = new NhanVienHelper(getApplicationContext());
		NhanVien nv = new NhanVien();

		nv.setTenNV(mEdtTenNV.getText() + "");
		nv.setGioiTinh(mGioiTinh);
		nv.setNgaySinh(mEdtNgaySinh.getText() + "");
		nv.setIdLoaiCongViec(mIdCongViec);
		if (mIdNhanVienSelected == -1) {
			db.addNhanVien(nv);
		} else {
			Log.d("SavaNhanVien", "doSaveNV mIdNhanVienSelected: "
					+ mIdNhanVienSelected);
			nv.setId(mIdNhanVienSelected);
			db.updateNhanVien(nv);
		}
		finish();

	}

	void doReset() {
		mEdtTenNV.setText("");
		mEdtNgaySinh.setText("");
		if (mRbNu.isChecked()) {
			mRbNu.setChecked(false);
			mRbNam.setChecked(true);
		}
		if (mRbFullTime.isChecked()) {
			mRbFullTime.setChecked(false);
			mRbPartTime.setChecked(true);
		}

	}

	void doCancel() {
		finish();
	}

	void chonNgaySinh() {
		Log.d("dd", "HungND");
		Calendar calendar = Calendar.getInstance();
		DatePickerDialog datePickerDialog = new DatePickerDialog(
				SaveNhanVienActivity.this, datePickerListener,
				calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));

		datePickerDialog.updateDate(calendar.get(Calendar.YEAR),
				calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));

		datePickerDialog.show();
	}

	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker datePicker, int year, int month,
				int day) {
			mEdtNgaySinh.setText(day + "/" + month + "/" + year);

		}
	};
}
